/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.managed;

import beans.ejb.FantasymatchFacade;
import beans.ejb.NflgameFacade;
import beans.ejb.WeekFacade;
import entities.Fantasyleague;
import entities.Fantasymatch;
import entities.Nflgame;
import entities.Week;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
/**
 *
 * @author stumbledog
 */
@ManagedBean
@SessionScoped
public class ScheduleBean implements Serializable {
    @EJB
    private FantasymatchFacade fantasyMathchFacade;
    @EJB
    private NflgameFacade nflGameFacade;
    @EJB
    private WeekFacade weekFacade;
    
    private Week currentWeek;

    public Week getCurrentWeek() {
        return currentWeek;
    }

    public void setCurrentWeek(Week currentWeek) {
        this.currentWeek = currentWeek;
    }
    private List<Fantasymatch> fantasyMathchList;
    private List<Nflgame> nflGameList;
    private Fantasyleague currentLeague;
    
    public ScheduleBean(){
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        AccountBean b = (AccountBean) request.getSession().getAttribute("accountBean");
        currentWeek = b.getCurrentWeek();
        currentLeague = b.getCurrentLeague();
        System.out.println("Schedule constructed");
    }
    
    
    
    public List<Fantasymatch> getFantasyMathchList() {
        fantasyMathchList = fantasyMathchFacade.findMatchResultByLeagueIdWeekId(currentLeague, currentWeek);
        return fantasyMathchList;
    }

    public void setFantasyMathchList(List<Fantasymatch> fantasyMathchList) {
        this.fantasyMathchList = fantasyMathchList;
    }
    
    public void previousWeek(){
        if(currentWeek.getWeek().intValue()>1){
            if(currentWeek.getWeek()<11){
                currentWeek = weekFacade.getWeek(currentWeek.getYear().toString(), "0"+String.valueOf(currentWeek.getWeek().intValue()-1));
            }else{
                currentWeek = weekFacade.getWeek(currentWeek.getYear().toString(), String.valueOf(currentWeek.getWeek().intValue()-1));
            }
        }
    }
    
    public void nextWeek(){
        if(currentWeek.getWeek().intValue()<14){
            if(currentWeek.getWeek()<9){
                currentWeek = weekFacade.getWeek(currentWeek.getYear().toString(), "0"+String.valueOf(currentWeek.getWeek().intValue()+1));
            }else{
                currentWeek = weekFacade.getWeek(currentWeek.getYear().toString(), String.valueOf(currentWeek.getWeek().intValue()+1));
            }
        }
    }
}
